import apiRequest from '@/utils/apiRequest';

/**
 * 获取采购入库单列表
 * @param {Object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {number} params.pageSize - 每页数量
 * @param {string} params.stockInCode - 入库单号
 * @param {string} params.stockInDate - 入库日期
 * @param {string} params.warehouseId - 仓库ID
 * @param {string} params.supplierId - 供应商ID
 * @param {string} params.purchaseOrderCode - 采购订单号
 * @param {string} params.status - 状态
 */
export const getPurchaseStockIns = async (params) => {
  try {
    const response = await apiRequest.get('/api/PurchaseStockIns', { params });
    return response.data;
  } catch (error) {
    console.error('Failed to get purchase stock ins:', error);
    throw error;
  }
};

/**
 * 获取采购入库单详情
 * @param {number} id - 采购入库单ID
 */
export const getPurchaseStockInDetail = async (id) => {
  try {
    const response = await apiRequest.get(`/api/PurchaseStockIns/${id}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get purchase stock in detail ${id}:`, error);
    throw error;
  }
};

/**
 * 创建采购入库单
 * @param {Object} stockInData - 采购入库单数据
 * @param {string} stockInData.stockInCode - 入库单号
 * @param {string} stockInData.stockInDate - 入库日期
 * @param {string} stockInData.warehouseId - 仓库ID
 * @param {string} stockInData.supplierId - 供应商ID
 * @param {string} stockInData.purchaseOrderId - 采购订单ID
 * @param {string} stockInData.remark - 备注
 * @param {Array} stockInData.details - 入库单明细
 */
export const createPurchaseStockIn = async (stockInData) => {
  try {
    const response = await apiRequest.post('/api/PurchaseStockIns', stockInData);
    return response.data;
  } catch (error) {
    console.error('Failed to create purchase stock in:', error);
    throw error;
  }
};

/**
 * 更新采购入库单
 * @param {number} id - 采购入库单ID
 * @param {Object} stockInData - 采购入库单数据
 * @param {string} stockInData.stockInCode - 入库单号
 * @param {string} stockInData.stockInDate - 入库日期
 * @param {string} stockInData.warehouseId - 仓库ID
 * @param {string} stockInData.supplierId - 供应商ID
 * @param {string} stockInData.purchaseOrderId - 采购订单ID
 * @param {string} stockInData.remark - 备注
 * @param {Array} stockInData.details - 入库单明细
 */
export const updatePurchaseStockIn = async (id, stockInData) => {
  try {
    const response = await apiRequest.put(`/api/PurchaseStockIns/${id}`, stockInData);
    return response.data;
  } catch (error) {
    console.error(`Failed to update purchase stock in ${id}:`, error);
    throw error;
  }
};

/**
 * 删除采购入库单
 * @param {number} id - 采购入库单ID
 */
export const deletePurchaseStockIn = async (id) => {
  try {
    const response = await apiRequest.delete(`/api/PurchaseStockIns/${id}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to delete purchase stock in ${id}:`, error);
    throw error;
  }
};

/**
 * 审核采购入库单
 * @param {number} id - 采购入库单ID
 * @param {Object} approveData - 审核数据
 * @param {string} approveData.approvalRemark - 审核备注
 */
export const approvePurchaseStockIn = async (id, approveData) => {
  try {
    const response = await apiRequest.patch(`/api/PurchaseStockIns/${id}/approve`, approveData);
    return response.data;
  } catch (error) {
    console.error(`Failed to approve purchase stock in ${id}:`, error);
    throw error;
  }
};

/**
 * 拒绝采购入库单
 * @param {number} id - 采购入库单ID
 * @param {Object} rejectData - 拒绝数据
 * @param {string} rejectData.rejectionRemark - 拒绝备注
 */
export const rejectPurchaseStockIn = async (id, rejectData) => {
  try {
    const response = await apiRequest.patch(`/api/PurchaseStockIns/${id}/reject`, rejectData);
    return response.data;
  } catch (error) {
    console.error(`Failed to reject purchase stock in ${id}:`, error);
    throw error;
  }
};

/**
 * 获取采购订单明细
 * @param {number} orderId - 采购订单ID
 */
export const getPurchaseOrderDetails = async (orderId) => {
  try {
    const response = await apiRequest.get(`/api/PurchaseOrders/${orderId}/details`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get purchase order details for ${orderId}:`, error);
    throw error;
  }
};